package com.ruoyi.core.service.impl;

import com.ruoyi.common.core.resource.OperatorUserSystem;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.core.service.VersionService;
import com.ruoyi.core.vo.VersionVo;
import com.ruoyi.framework.utils.SessionHelper;
import com.ruoyi.system.domain.SysVersion;
import com.ruoyi.system.mapper.SysVersionMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @Author xiaom
 * @Date 2020/12/19 15:13
 * @Version 1.0.0
 * @Description <>
 **/
@Service
public class VersionServiceImpl implements VersionService {

    @Resource
    private SysVersionMapper sysVersionMapper;

    @Override
    public SysVersion getLastVersion() {
        return sysVersionMapper.selectLastVersion();
    }

    @Override
    public Boolean addVersion(VersionVo versionVo) {
        SysVersion sysVersion;
        if(versionVo.getId() != null){
            Long versionId = versionVo.getId();
            //更新操作
            sysVersion = sysVersionMapper.selectByPrimaryKey(versionId);
            if(sysVersion == null){
                throw new CustomException("不存在的版本,更新失败");
            }
            sysVersion.setCreateTime(new Date());
            BeanUtils.copyBeanProp(sysVersion,versionVo);
            sysVersion.setId(versionId);
            return sysVersionMapper.updateByPrimaryKey(sysVersion) == 1;
        }else{
            sysVersion = new SysVersion();
            BeanUtils.copyBeanProp(sysVersion,versionVo);
            if(sysVersionMapper.selectByVersionNo(versionVo.getVersion()) != null){
                throw new CustomException("版本号:["+versionVo.getVersion()+"]已存在");
            }
            sysVersion.setCreateTime(new Date());
            return sysVersionMapper.insertSelective(sysVersion) == 1;
        }
    }

    @Override
    public List<SysVersion> getList() {
        return sysVersionMapper.selectList();
    }

    @Override
    public Boolean deleteVersion(Long id) {
        return sysVersionMapper.deleteByPrimaryKey(id) == 1;
    }
}
